package com.dao.impl;

import com.dao.BaseAdminDao;
import com.util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

public class AdminDaoMysql extends DBUtils implements BaseAdminDao {
    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE eadmin SET logintime=?,loginip=? WHERE username=?";
    private static final String SQL_LOGIN = "SELECT id FROM eadmin WHERE username=? AND password=?";

    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Date(date.getTime()));
        arrayList.add(ip);
        arrayList.add(username);
        try {
            update(SQL_UPDATE_LOGIN_TIME, arrayList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
    }

    @Override
    public boolean login(String username, String password) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(username);
        arrayList.add(password);
        resultSet = query(SQL_LOGIN, arrayList);
        try {
            return resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
